package com.yuqing.sentimentapp.home.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.linkface.baselibrary.utils.DensityUtil;
import com.linkface.common.adaper.CommonViewHolder;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseFragment;
import com.yuqing.sentimentapp.home.acitivity.NewsDetailActivity;
import com.yuqing.sentimentapp.home.adapter.EarlyWarningAdapter;
import com.yuqing.sentimentapp.home.bean.EarlyWarningDataBean;
import com.yuqing.sentimentapp.home.presenter.EarlyWarningPresenter;
import com.yuqing.sentimentapp.home.presenter.impl.EarlyWarningPresenterImpl;
import com.yuqing.sentimentapp.home.view.EarlyWarningView;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.widget.RecycleViewDivider;
import com.yuqing.sentimentapp.widget.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 预警页
 * Created by ShenHe on 2019/4/11.
 */

public class EarlyWarningFragment extends BaseFragment implements EarlyWarningView<EarlyWarningDataBean> {

    @BindView(R.id.recyclerView)
    XRecyclerView mRecyclerView;
    private EarlyWarningAdapter mAdapter;
    private List<EarlyWarningDataBean.DataBean> dataList = new ArrayList<>();
    private EarlyWarningPresenter mPresenter;
    private int page = 1;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_early_warning;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initRecyclerView();
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setPullRefreshEnabled(true);

        mAdapter = new EarlyWarningAdapter(getActivity(), dataList, new CommonViewHolder.onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                EarlyWarningDataBean.DataBean dataBean = dataList.get(position - 1);
                NewsDetailActivity.start(getActivity(), dataBean.getUrl(), dataBean.getTitle(), true);
                dataList.get(position - 1).setIsRead("1");
                mAdapter.notifyItemChanged(position);
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        });
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData(page, true);
            }

            @Override
            public void onLoadMore() {
                page++;
                loadData(page, false);
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            loadData(page, true);
        }
    }

    private void loadData(int page, boolean isRefresh) {
        mPresenter.loadEarlyWarningData(page, 10, isRefresh);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new EarlyWarningPresenterImpl(this);
    }

    @Override
    public void onSuccess(EarlyWarningDataBean data) {
        stopLoad();
    }

    @Override
    public void onFailure(String message) {
        stopLoad();
        ToastUtil.showShort(message);
    }

    @Override
    public void returnEaylyWarningResult(EarlyWarningDataBean result, boolean isRefresh) {
        stopLoad();
        if (result == null) return;
        if (null == result.getData() || result.getData().size() == 0) {
            if (!isRefresh) {
                mRecyclerView.setNoMore(true);
            }
            return;
        }
        if (isRefresh) {
            dataList.clear();
        }
        dataList.addAll(result.getData());
        mAdapter.notifyDataSetChanged();
    }

    private void stopLoad() {
        if (mRecyclerView != null) {
            mRecyclerView.refreshComplete();
            mRecyclerView.loadMoreComplete();
        }
    }
}
